%%%-------------------------------------------------------------------
%%% @author Viktor Green <viktor@student.gu.se>
%%% @copyright (C) 2011, Viktor Green
%%% @doc
%%% Tests for module bencode.erl
%%% @end
%%% Created :  7 Dec 2011 by Viktor Green <viktor@student.gu.se>
%%%-------------------------------------------------------------------
-module(bencode_tests).

-include_lib("eunit/include/eunit.hrl").

bencode_test_() ->
    [fun encode_integer/0,
     fun encode_string/0,
     fun encode_list/0,
     fun encode_dictionary/0,
     fun decode_integer/0,
     fun decode_string/0,
     fun decode_list/0,
     fun decode_dictionary/0
    ].

encode_integer() ->
    ?assert("i42e" == bencode:encode({integer, 42})),
    ?assert("i0e" == bencode:encode({integer, 0})),
    ?assert("i-42e" == bencode:encode({integer, -42})),
    ?assertNot("i-0e" == bencode:encode({integer, -0})).

encode_string() ->
    ?assert("4:spam" == bencode:encode({string, "spam"})),
    ?assert("0:" == bencode:encode({string, ""})).

encode_list() ->
    ?assert("l4:spami42ee" == bencode:encode({list,[{string, "spam"}, {integer, 42}]})),
    ?assert("le" == bencode:encode({list, []})).

encode_dictionary() ->
    ?assert("d3:bar4:spam3:fooi42ee" == bencode:encode({dictionary, [{{string, "bar"}, {string, "spam"}}, {{string,"foo"}, {integer, 42}}]})).

decode_integer() ->
    ?assert({integer, 42} == bencode:decode("i42e")),
    ?assert({integer, 0} == bencode:decode("i0e")),
    ?assert({integer, -42} == bencode:decode("i-42e")).

decode_string() ->
    ?assert({string, "spam"} == bencode:decode("4:spam")),
    ?assert({string, ""} == bencode:decode("0:")).

decode_list() ->
    ?assert({list, [{string, "spam"}, {integer, 42}]} == bencode:decode("l4:spami42ee")),
    ?assert({list, []} == bencode:decode("le")).

decode_dictionary() ->
    ?assert({dictionary, [{{string, "bar"}, {string, "spam"}}, {{string, "foo"}, {integer, 42}}]} == bencode:decode("d3:bar4:spam3:fooi42ee")).
